<?php
/**
 * Created by PhpStorm.
 * User: mqq
 * Date: 2016/4/24
 * Time: 21:32
 */

namespace Addons\SchoolNews\Controller;
use DiDom\Document;
use Home\Controller\AddonsController;
use Org\Util\Snoopy;

class NewsController extends AddonsController{
    public function _initialize()
    {
        vendor("DiDom.Document");
        vendor("DiDom.Query");
        vendor("DiDom.Element");
    }

    public function index(){
        $BaseUrl="http://gxsdxy.cn/content/";
        $href=I("get.href");
        $url=$BaseUrl.$href;
        $sn = new Snoopy();
        $sn->fetch($url);
        $document = new Document($this->formaturl($sn->results, "http://gxsdxy.cn/"));
        $posts = $document->find('.node-content')[0];
        $title=$document->find('#page-title')[0];
        $cat=$document->find('.node-property')[0];
        $this->assign("content",trim($this->base64img($posts->html())));
        $this->assign("title",$title->text());
        $this->assign("cat",trim($cat->text()));
        $this->display();
    }

    public function base64img($html){
        $sn=new Snoopy();
        $document=new Document($html);
        $posts=$document->find("img");
        foreach ($posts as $post){
            $img=$post->src;
            $sn->fetch($post->src);
            $tmp=base64_encode($sn->results);
//            $html=preg_replace('/(<img.*?)border=(["\'])?.*?(?(2)\2|\s)([^>]+>)/is',"'$1$3'",$html);
            $html = preg_replace("/height\=\"[0-9]+?\"|width\=\"[0-9]+?\"/","style=\"width: 100%; height: auto;max-width: 100%; display: block;\"",$html);
            $html=str_replace($img,"data:image/jpg;base64,$tmp",$html);
//            $html=preg_replace("/src=\"$img\"/","src=\"data:image/jpg;base64,$tmp\"",$html);
        }
        return $html;
    }

    public function formaturl($l1,$l2){
        if (preg_match_all("/(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<a[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src='([^']+)'[^>]*>)|(<a[^>]+href='([^']+)'[^>]*>)/i",$l1,$regs)){
            foreach($regs[0] as $num => $url){
                $l1 = str_replace($url,$this->lIIIIl($url,$l2),$l1);
            }
        }
        return $l1;
    }
    public function lIIIIl($l1,$l2){
        if(preg_match("/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i",$l1,$regs)){$I2 = $regs[3];}
        if(strlen($I2)>0){
            $I1 = str_replace(chr(34),"",$I2);
            $I1 = str_replace(chr(39),"",$I1);
        }else{return $l1;}
        $url_parsed = parse_url($l2);
        $scheme = $url_parsed["scheme"];if($scheme!=""){$scheme = $scheme."://";}
        $host = $url_parsed["host"];
        $l3 = $scheme.$host;
        if(strlen($l3)==0){return $l1;}
        $path = dirname($url_parsed["path"]);if($path[0]=="\\"){$path="";}
        $pos = strpos($I1,"#");
        if($pos>0) $I1 = substr($I1,0,$pos);
//判断类型
        if(preg_match("/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&)|&)+/i",$I1)){return $l1; }//http开头的url类型要跳过
        elseif($I1[0]=="/"){$I1 = $l3.$I1;}//绝对路径
        elseif(substr($I1,0,3)=="../"){//相对路径
            while(substr($I1,0,3)=="../"){
                $I1 = substr($I1,strlen($I1)-(strlen($I1)-3),strlen($I1)-3);
                if(strlen($path)>0){
                    $path = dirname($path);
                }
            }
            $I1 = $l3.$path."/".$I1;
        }
        elseif(substr($I1,0,2)=="./"){
            $I1 = $l3.$path.substr($I1,strlen($I1)-(strlen($I1)-1),strlen($I1)-1);
        }
        elseif(strtolower(substr($I1,0,7))=="mailto:"||strtolower(substr($I1,0,11))=="javascript："){
            return $l1;
        }else{
            $I1 = $l3.$path."/".$I1;
        }
        return str_replace($I2,"\"$I1\"",$l1);
    }

}